package com.itqf.factory;

import java.io.IOException;
import java.util.Properties;

/**
 * @Description: 我的工厂类   创建对象  UserDao dao = new UsersDaoImpl();
 * @Company: 千锋互联
 * @Author: 李丽婷
 * @Date: 2020/11/9
 * @Time: 上午9:54
 */
public class MyFactory {
     Properties properties = new Properties();

    public MyFactory(){

    }

    /**
     * 读取xxx.properties
     * @param configFile
     */
    public  MyFactory(String configFile){
        try {
            //getClassLoader()  加载项目的classpath(classes)下的xxx文件
            properties.load(MyFactory.class.getClassLoader().getResourceAsStream(configFile));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取对象
     * @param beanName  对象的key    userDao=com.itqf.dao.impl.UsersDaoImpl
     * @return
     */
    public    Object getBean(String beanName) throws  Exception{
        //com.itqf.dao.impl.UsersDaoImpl
        String value = properties.getProperty(beanName);
        if (value!=null) {
            //根据类的全路径构建类的Class对象
            Class clazz = Class.forName(value);
            //通过反射创建对象
            Object object = clazz.newInstance();
            return  object;
        }

        return  null;
    }




}
